package com.jg.utils.reflex.classes;

import com.jg.utils.reflex.en.User;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Constructor;

public class ClassNewInstanceTest {
    public void testClassNewInstance() throws Exception{
        Class<User> userClass = User.class;
        //通过调用空参数构造成功创建对象
        //Class中的newInstance
        User user = userClass.newInstance();
        System.out.println(user);
    }
    @Test
    public void testConstructor() throws Exception{
        Class<User> userClass = User.class;
        Constructor<User> declaredConstructor = userClass.getDeclaredConstructor(String.class, Integer.class, String.class);
        User user = declaredConstructor.newInstance("张三", 24, "244");
        System.out.println(user);
//constructor的newInstance 还可以通过私有构造方法去创建对象。但是这个及不安全。
        Constructor<User> declaredConstructor1 = userClass.getDeclaredConstructor(String.class);
        declaredConstructor1.setAccessible(true);
        User user1 = declaredConstructor1.newInstance("张三");
        System.out.println(user1);

    }


}
